#include "main.h"
#include "uart.h"
#include "led.h"
#include "key.h"
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
#include "tim.h"

char disp[20];	

extern int time;
int hour = 1,sec = 10,min = 25;
int temp_hour = 0,temp_sec = 0,temp_min = 0;
int countdown_time = 0;

int pa1_duty_cycle = 8;
int pa2_duty_cycle = 1;
int channel = 1;
int commmand_flag = 0;
int pwm_flag = 0;
int low_flag = 0;
int countdown_time_flag = 0;

void SystemClock_Config(void);

void KEY_Control(void);		
void LCD_Control(void);	
void Led_Control(void);
void TIME_Control(void);
void UART_Control(void);
void PWM_Control(void);
void Base_Disp(void);


int Car_Control(int mode,int index_id);

int main(void)
{

  HAL_Init();
	
  SystemClock_Config();
	MX_USART1_Init();
	
	KEY_Init();
	LED_Init();
	LCD_Init();	
	TIM2_Init();
	
	LCD_Clear(White);
	LCD_SetBackColor(White);
	LCD_SetTextColor(Black);
	LED_Control(LED_ALL,0);
	

  while (1)
  {
		LCD_Control(); 	
		KEY_Control();	
		TIME_Control();	
		Led_Control();	
		UART_Control();	
		PWM_Control();
  }
}


void SystemClock_Config(void){
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
 
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV2;
  RCC_OscInitStruct.PLL.PLLN = 20;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the peripherals clocks 
  */
}




void UART_Control(void){
	if(UART1_Rx_flg){
		//Test
		Uart_SendString(UART1_Rx_Buf);
		commmand_flag = 1;
		temp_hour = (UART1_Rx_Buf[0] - '0')*10 + (UART1_Rx_Buf[1] - '0');
		temp_min = (UART1_Rx_Buf[3] - '0')*10 + (UART1_Rx_Buf[4] - '0');
		temp_sec = (UART1_Rx_Buf[6] - '0')*10 + (UART1_Rx_Buf[7] - '0');
		channel = (UART1_Rx_Buf[11] - '0');
		countdown_time = (UART1_Rx_Buf[13] - '0');
		
		HAL_UART_Receive_IT(&huart1,(uint8_t *)UART1_temp,1);
		UART1_Rx_flg = 0;
	}
}
void Duty_Cycle_Control(int * duty_cycle){
	*duty_cycle+=1;
	pwm_flag = 1;
	if(*duty_cycle == 11){
		*duty_cycle = 0;
	}
}

void PWM_OR_LOW(int *flag){
	*flag = !*flag;
	if(*flag)
		low_flag = 1;
	else
		pwm_flag = 1;
}

int pa1_set_flag = 1;
int pa2_set_flag = 1;

void KEY_Control(){
	if(KEY_Scan()==1){
		channel = 1;
		PWM_OR_LOW(&pa1_set_flag);
	}
	if(KEY_Scan()==2){
		Duty_Cycle_Control(&pa1_duty_cycle);
	}
	if(KEY_Scan()==3){
		channel = 2;
		PWM_OR_LOW(&pa2_set_flag);
	}
	if(KEY_Scan()==4){
		Duty_Cycle_Control(&pa2_duty_cycle);
	}
}



void Led_Control(){
	
}


void TIME_Control(void){
	if(sec==60){
		sec=0;
		min+=1;
	}
	if(min==60){
		min=0;
		hour+=1;
	}
	if(hour==24)
		hour=0;
	//如果处于命令模式那就每秒对比
	if(commmand_flag){
		if(temp_hour == hour && temp_min == min && temp_sec == sec){
			pwm_flag = 1;
		  countdown_time_flag = 1;
		}
		if(countdown_time == 0){
			countdown_time_flag = 0;
			commmand_flag = 0;
			low_flag = 1;
		}
	}
}

void PWM_Control(){
	if(pwm_flag){
		if(channel){
			TIM2_SetCompare(TIM_CHANNEL_2,pa1_duty_cycle*100); //占空比设为40%
		}
		if(channel==2)
		{
			TIM2_SetCompare(TIM_CHANNEL_3,pa2_duty_cycle*100); //占空比设为40%
		}

		Uart_SendString("set pwm");
		pwm_flag = 0;
	}
	if(low_flag){
		if(channel){
			TIM2_SetCompare(TIM_CHANNEL_2,0);
		}
		if(channel==2){
			TIM2_SetCompare(TIM_CHANNEL_3,0);
		}
		Uart_SendString("set low");
		low_flag = 0;
	}
	
}
void Base_Disp(){
		
	sprintf(disp,"PWM-PA1:%2d%%   ",pa1_duty_cycle*10);
	LCD_DisplayStringLine(Line2,disp);

	sprintf(disp,"PWM-PA2:%2d%%   ",pa2_duty_cycle*10);
	LCD_DisplayStringLine(Line3,disp);
	
	sprintf(disp,"Time:%2d-%2d-%2d   ",hour,min,sec);
	LCD_DisplayStringLine(Line4,disp);
	
	sprintf(disp,"Channel:PA%d   ",channel);
	LCD_DisplayStringLine(Line5,disp);


	sprintf(disp,"Command:   ",channel);
	LCD_DisplayStringLine(Line6,disp);
	
	if(commmand_flag){
		sprintf(disp,"%2d:%2d:%2d-PA%d-%dS   ",temp_hour,temp_min,temp_sec,channel,countdown_time);
		LCD_DisplayStringLine(Line7,disp);
	}else{
		LCD_DisplayStringLine(Line7,"NONE                 ");
	}
}



void LCD_Control(){
	 Base_Disp();
}








void Error_Handler(void){}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line){ }
#endif
